package com.blb.thread;

public class Demo5StopThread {
    private static boolean flag= false;
    public static void main(String[] args) {

        new Thread(() ->{
            for (int i = 0;i<=100;i++){
                if (flag){
                    System.out.println("线程停止");
                    break;
                }
                System.out.println(Thread.currentThread().getName()+"--->"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        //主线程5秒停止子线程
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag=true;
    }
}
